//
//  CenterPageViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CenterPageViewController.h"
#import "CenterFirstCell.h"
#import "CenterTwoCell.h"
#import "CenterLoopView.h"
#import "CenterLoopViewTwo.h"
#import "CenterLoopBottomView.h"
#import "EVNCustomSearchBar.h"
#import "ContainerViewController.h"
#import "OrganizationViewController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h>
#import <BaiduMapAPI_Map/BMKAnnotation.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Map/BMKPinAnnotationView.h>
#import "LocaltionChooseViewController.h"
#import "CenterpageSearchResultViewController.h"
#import "CompetitionViewController.h"
#import "InstitutionsDetailContainViewController.h"
#import "SignUpViewController.h"
#import "CustomGuideView.h"
@interface CenterPageViewController ()<UITableViewDelegate,UITableViewDataSource,EVNCustomSearchBarDelegate,BMKLocationServiceDelegate,UIViewControllerTransitioningDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *imagesArra;
@property(nonatomic,strong)NSArray *titlesArra;
@property(nonatomic,strong)EVNCustomSearchBar * searchBar;
@property(nonatomic,strong)BMKLocationService *service;
@property(nonatomic,strong)UIButton *addressBtn;
@property (strong,nonatomic) CustomGuideView *guideView;
@property (assign,nonatomic) BOOL isFirstLaunch;
@end

@implementation CenterPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initSearchBar];
    self.service = [[BMKLocationService alloc] init];
    self.service.delegate = self;
    [self.service startUserLocationService];
    [self createCollectionView];
    // Do any additional setup after loading the view.
}
-(void)createNavagationBackBtn{
    self.addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addressBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_addressBtn setImage:[UIImage imageNamed:@"localtion"] forState:UIControlStateNormal];
    [_addressBtn addTarget:self action:@selector(CityChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    _addressBtn.frame = CGRectMake(0, 0, scaleLeftAndRight(60), scaleTopAndBottom(15));
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:_addressBtn];
    self.navigationItem.leftBarButtonItem = leftBar;

}


-(void)CityChooseAction:(UIButton *)btn{
    LocaltionChooseViewController * picker = [[LocaltionChooseViewController alloc] init];
    picker.hotCitys = @[@"上海",@"北京",@"广州",@"深圳",@"杭州",@"天津"];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:picker];
    picker.selectedAction = ^(NSString * city,LHCityPickerController * picker){
        NSLog(@"选中城市:%@",city);
        [btn setTitle:city forState:UIControlStateNormal];
        [picker dismissViewControllerAnimated:YES completion:nil];
    };
    picker.cityTile = btn.titleLabel.text;
    nav.transitioningDelegate = self;
    nav.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nav animated:YES completion:nil];
}
-(void)createCollectionView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[CenterFirstCell class] forCellReuseIdentifier:@"CenterFirstCell"];
    [self.tableView registerClass:[CenterTwoCell class] forCellReuseIdentifier:@"CenterTwoCell"];
    [self.view addSubview:self.tableView];
        if (@available(iOS 11.0, *)) {
            self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    if (indexPath.section == 0) {
        CenterFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CenterFirstCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.pageTransformBlockOne = ^{
            self.tabBarController.selectedIndex = 1;
        };
        cell.pageTransformBlockTwo = ^{
            self.tabBarController.selectedIndex = 1;
        };
        cell.pageTransformBlockThree = ^{
            OrganizationViewController *orgationVc = [[OrganizationViewController alloc] init];
            [self.navigationController pushViewController:orgationVc animated:YES];
        };
        return cell;
    }
    if (indexPath.section == 1) {
        CenterTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CenterTwoCell"];
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 2) {
        CenterLoopView *loopView = [[CenterLoopView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(165))];
        loopView.valueBlock = ^(MatchListModel *model) {
            SignUpViewController *signupVc = [[SignUpViewController alloc] init];
            signupVc.matchID = model.Id;
            signupVc.matchStarttime = model.submitEndTime;
            [self.navigationController pushViewController:signupVc animated:YES];
        };
        [cell.contentView addSubview:loopView];
    }
    if (indexPath.section == 3) {
        CenterLoopViewTwo *loopView = [[CenterLoopViewTwo alloc] initWithFrame:CGRectMake(0, 0, WIDTH, scaleTopAndBottom(165))];
        loopView.detailMatchBlock = ^(NSString *matchId) {
            InstitutionsDetailContainViewController *vc = [[InstitutionsDetailContainViewController alloc] init];
            vc.instituId = matchId;
            [self.navigationController pushViewController:vc animated:YES];
        };
        [cell.contentView addSubview:loopView];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return scaleTopAndBottom(360.f);
    }else if (indexPath.section ==1){
        return scaleTopAndBottom(60.f);
    }else if (indexPath.section == 2){
        return scaleTopAndBottom(180);
    }else{
         return scaleTopAndBottom(180);
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *viwe = [[UIView alloc] init];
    if (section == 2 || section == 3) {
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.image = [UIImage imageNamed:self.imagesArra[section - 2]];
        [viwe addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(viwe.mas_left);
            make.top.equalTo(viwe.mas_top).offset(scaleTopAndBottom(15));
            make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(84), scaleTopAndBottom(30)));
        }];
//        UILabel *tielLabel = [[UILabel alloc] init];
//        tielLabel.font = [UIFont systemFontOfSize:14];
//        tielLabel.adjustsFontSizeToFitWidth = YES;
//        tielLabel.textColor = [UIColor whiteColor];
//        tielLabel.text = self.titlesArra[section - 2];
//        [imageV addSubview:tielLabel];
//        [tielLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(imageV.mas_left).offset(12);
//            make.right.equalTo(imageV.mas_right).offset(-12);
//            make.top.bottom.equalTo(imageV);
//        }];
        UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        moreBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [moreBtn setTitleColor:COLOR_HEX(0x121212) forState:UIControlStateNormal];
        [viwe addSubview:moreBtn];
        [moreBtn setTitle:@"查看更多>" forState:0];
        moreBtn.tag = 10000 + section;
        [moreBtn addTarget:self action:@selector(pageTransform:) forControlEvents:UIControlEventTouchUpInside];
        [moreBtn setTitleColor:[UIColor blackColor] forState:0];
        [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(viwe.mas_top).offset(scaleTopAndBottom(20));
            make.size.mas_equalTo(CGSizeMake(scaleLeftAndRight(100), scaleTopAndBottom(21)));
            make.right.equalTo(viwe.mas_right).offset(scaleLeftAndRight(0));
        }];
    }
    return viwe;
}

/**
 按钮的页面跳转

 
 */
-(void)pageTransform:(UIButton *)btn{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    if (btn.tag == 10002) {
        ContainerViewController *containVc = [[ContainerViewController alloc] init];
        [self.navigationController pushViewController:containVc animated:YES];
    }
    if (btn.tag == 10003) {
        OrganizationViewController *orgationVc = [[OrganizationViewController alloc] init];
        [self.navigationController pushViewController:orgationVc animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 1) {
        return 0.01f;
    }else{
        return scaleTopAndBottom(55.f);
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = COLOR_HEX(0xf4f5f6);
    return footerView;
}

#pragma mark - UIScrollViewDelegate和取消区头的停滞效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]){
    }
//    if (scrollView.contentOffset.y > scaleTopAndBottom(175) - 64) {
//        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//        [self.navigationController.navigationBar setShadowImage:nil];
//    }
//    if (scrollView.contentOffset.y < scaleTopAndBottom(175) - 64) {
//
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; //去除 navigationBar 底部的细线
//        self.navigationController.navigationBar.shadowImage = [UIImage new];
//    }
    CGFloat offset = self.tableView.contentOffset.y;
    [self.navigationController.navigationBar
     setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:201/255.0 blue:31/255.0 alpha:(offset / 64)>0.99?0.99:(offset / 64)]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar
     setShadowImage:[UIImage new]];
    
    
//    CGFloat minAlphaOffset = 0;
//    CGFloat maxAlphaOffset = scaleTopAndBottom(175);
//    CGFloat offset = scrollView.contentOffset.y;
//    CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
//    self.navigationController.navigationBar.subviews.firstObject.alpha = alpha;
    CGFloat sectionHeaderHeight = scaleTopAndBottom(45.f);
    if (scrollView.contentOffset.y <=sectionHeaderHeight && scrollView.contentOffset.y >=  0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 4) {
        return 0.01f;
    }
    return 8.0f;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"]) {
        _isFirstLaunch = YES;
        [[NSUserDefaults standardUserDefaults] setBool:_isFirstLaunch forKey:@"isFirstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.guideView = [[CustomGuideView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        __weak typeof(self) weakSelf = self;
        _guideView.closeAndInBlock = ^{
            [UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                weakSelf.guideView.alpha = 0;
            } completion:^(BOOL finished) {
                [weakSelf.guideView removeFromSuperview];
            }];
        };
        [[UIApplication sharedApplication].keyWindow addSubview:_guideView];
//        [self.view addSubview:_guideView];
    }
    CGFloat offset = self.tableView.contentOffset.y;
    [self.navigationController.navigationBar
     setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:201/255.0 blue:31/255.0 alpha:(offset / 64)>0.99?0.99:(offset / 64)]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar
     setShadowImage:[UIImage new]];
    self.tabBarController.tabBar.hidden = NO;
//    CGFloat minAlphaOffset = 0;
//    CGFloat maxAlphaOffset = scaleTopAndBottom(175);
//    CGFloat offset = self.tableView.contentOffset.y;
//    CGFloat alpha = (offset - minAlphaOffset) / (maxAlphaOffset - minAlphaOffset);
//    self.navigationController.navigationBar.subviews.firstObject.alpha = alpha;
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
   
}


-(UIImage*) imageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
    
}
//-(void)createSearchbarControllerInView:(UIView *)view{
//    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 257, 25)];
//    self.searchBar.placeholder = @"搜索活动/赛事/机构";
//    self.searchBar.showsCancelButton = YES;
//    [view addSubview:self.searchBar];
//}
/**
 创建搜索框
 */
//-(void)createSearchbarControllerInView:(UIView *)view{
//    NSLog(@"挖煤肚子");
//    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//    self.searchController.delegate = self;
//    self.searchController.searchResultsUpdater = self;
//    //    _searchController.dimsBackgroundDuringPresentation = NO;
//    self.searchController.obscuresBackgroundDuringPresentation = YES;
//    self.searchController.hidesNavigationBarDuringPresentation = NO;
//    self.searchController.searchBar.placeholder = @"搜索活动/赛事/机构";
//    self.searchController.searchBar.barStyle = UIBarStyleBlack;
//    [self.searchController.searchBar setValue:@"取消" forKeyPath:@"_cancelButtonText"];
//    UITextField *searchField = [_searchController.searchBar valueForKey:@"_searchField"];
//    [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//    [self.searchController.searchBar setImage:[UIImage imageNamed:@"sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
//    self.searchController.searchBar.frame = CGRectMake(0, 0, 257, 25);
//    [view addSubview:self.searchController.searchBar];
////    [self.view addSubview:self.searchController.searchBar];
//}

-(NSArray *)imagesArra{
    if (_imagesArra == nil) {
        _imagesArra = @[@"CenterpageCompetion",@"CenterpageInsitute",@"Centerpageexpericence"];
    }
    return _imagesArra;
}
-(NSArray *)titlesArra{
    if (_titlesArra == nil) {
        _titlesArra = @[@"热门赛事",@"机构推荐",@"体验活动"];
    }
    return _titlesArra;
}
#pragma mark: 设置顶部导航搜索部分

- (void)initSearchBar
{
//    UIButton *addressBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [addressBtn setTitle:@"上海" forState:0];
//    addressBtn.titleLabel.font = [UIFont systemFontOfSize:13];
//    [addressBtn setImage:[UIImage imageNamed:@"centerPageadd"] forState:UIControlStateNormal];
//    addressBtn.frame = CGRectMake(0, 0, scaleLeftAndRight(38), scaleTopAndBottom(15));
//    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:addressBtn];
//    self.navigationItem.leftBarButtonItem = leftBar;
    UIButton *emailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"centeremail"] forState:UIControlStateNormal];
    [self.view addSubview:emailBtn];
    emailBtn.frame = CGRectMake(0, 0, scaleLeftAndRight(19), scaleTopAndBottom(15));
    UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithCustomView:emailBtn];
    self.navigationItem.rightBarButtonItem = rightBar;
    self.navigationItem.titleView = self.searchBar;
    if (@available(iOS 11.0, *))
    {
        [self.searchBar.heightAnchor constraintLessThanOrEqualToConstant:44].active = YES;
    }
    else
    {
        
    }
    
    
}
#pragma mark: getter method EVNCustomSearchBar
- (EVNCustomSearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 30)];
//        _searchBar.layer.borderWidth = 1;
//        _searchBar.layer.borderColor = [UIColor grayColor].CGColor;
//        _searchBar.backgroundColor = [UIColor clearColor]; // 清空searchBar的背景色
        NSLog(@"图片的信息文字%@" ,[UIImage imageNamed:@"centerpagesousuo.png"]);
        _searchBar.iconAlign = EVNCustomSearchBarIconAlignLeft;
        [_searchBar setPlaceholder:@"搜索活动/赛事/机构"];  // 搜索框的占位符
        _searchBar.delegate = self; // 设置代理
        _searchBar.isHiddenCancelButton = YES;
//        [_searchBar becomeFirstResponder];
        [_searchBar sizeToFit];
    }
    return _searchBar;
}

- (UIImage *)imagesNamedFromCustomBundle:(NSString *)imgName
{
    NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"EVNCustomSearchBar.bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *img_path = [bundle pathForResource:imgName ofType:@"png"];
    return [UIImage imageWithContentsOfFile:img_path];
}
#pragma mark: EVNCustomSearchBar delegate method
- (BOOL)searchBarShouldBeginEditing:(EVNCustomSearchBar *)searchBar
{

    CenterpageSearchResultViewController *resultVc = [[CenterpageSearchResultViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:resultVc];
    nav.transitioningDelegate = self;
    nav.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:nav animated:YES
                     completion:^{
      [self.searchBar resignFirstResponder];
                     }];
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
    NSLog(@"%@",searchBar.text);
    return YES;
}

//- (void)searchBarTextDidBeginEditing:(EVNCustomSearchBar *)searchBar
//{
//    NSLog(@"%@",searchBar.text);
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//
//}
//
//- (BOOL)searchBarShouldEndEditing:(EVNCustomSearchBar *)searchBar
//{
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//    return YES;
//}
//
//- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar
//{
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//}
//
//- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText
//{
//    NSLog(@"搜索到的字%@",searchText);
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//}
//
//- (BOOL)searchBar:(EVNCustomSearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//    return YES;
//}
//
//- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar
//{
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//}
//
//- (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar
//{
//    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self.searchBar resignFirstResponder];
//}
/**当获取到定位的坐标后，回调函数*/

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    BMKCoordinateRegion region;

    region.center.latitude  = userLocation.location.coordinate.latitude;
    
    region.center.longitude = userLocation.location.coordinate.longitude;
    
    region.span.latitudeDelta = 0;
    
    region.span.longitudeDelta = 0;
    
    NSLog(@"当前的坐标是:%f,%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    NSString *latitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude ];
    NSString *longtitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
    
    [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"latitude"];
    
    [[NSUserDefaults standardUserDefaults] setObject:longtitude forKey:@"longtitude"];
    
    
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    
    [geocoder reverseGeocodeLocation: userLocation.location completionHandler:^(NSArray *array, NSError *error) {
        
        if (array.count > 0) {
            
            CLPlacemark *placemark = [array objectAtIndex:0];
            
            if (placemark != nil) {
                
                NSString *city = placemark.locality;
                
                [self createNavagationBackBtn];
                [self.addressBtn setTitle:city forState:UIControlStateNormal];
                [[NSUserDefaults standardUserDefaults] setObject:city forKey:@"currentLocaltion"];
                NSLog(@"当前城市名称------%@",city);
                
                //找到了当前位置城市后就关闭服务
                
                [self.service stopUserLocationService];
            }
            
        }
        
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
